package com.tonggedata.dataapi.service.impl;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.tonggedata.dataapi.service.ApiDataBaseService;
import com.tonggedata.dataapi.util.ApiServiceUtil;
import com.tonggedata.dataapi.util.JDBCUtils;
import com.tonggedata.dataapi.util.PropertiesUtils;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ApiDatabaseServiceImpl implements ApiDataBaseService{

    @Override
    public Map<String, Object> getAllDatabase() {
        Map<String, Object> result = new HashMap<>();
        Properties pro;
        try {
            pro = new PropertiesUtils().getProperties();
            pro.setProperty("dbname","information_schema");
        } catch (IOException e) {
            return null;
        }
        DruidPooledConnection connection = null;
        try {
            connection = JDBCUtils.getJdbcConnectionPool(pro);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        List<String> list = new ArrayList<>();
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT `SCHEMA_NAME` FROM `SCHEMATA`");

            while (resultSet.next()){
                String s = resultSet.getString(1);
                list.add(s);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        result.put(Constants.DATA_LIST, list);
        ApiServiceUtil.putMsg(result, Status.SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> getAllTables(String databaseName) {
        Map<String, Object> result = new HashMap<>();

        Properties pro;
        try {
            pro = new PropertiesUtils().getProperties();
            pro.setProperty("dbname",databaseName);
        } catch (IOException e) {
            return null;
        }
        DruidPooledConnection connection = null;
        ArrayList<String> tables = new ArrayList<>();
        try {
            connection = JDBCUtils.getJdbcConnectionPool(pro);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("show tables");
            while(resultSet.next()){
                String tableName = resultSet.getString(1);
                tables.add(tableName);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        result.put(Constants.DATA_LIST, tables);
        ApiServiceUtil.putMsg(result, Status.SUCCESS);
        return result;
    }

    @Override
    public Result getAllColumns(String tableName, String databaseName) {
        return null;
    }
}
